﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.Student;
using StartsEAS_API.Model;

namespace StartsEAS_API.Controllers.StudentContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class StudentController : ControllerBase
    {
        private readonly IStudentBLL _studentBll;
        public StudentController(IStudentBLL studentBLL) { 
            _studentBll = studentBLL;
        }
        /// <summary>
        /// 新增学生信息
        /// </summary>
        /// <param name="data">学生信息实体JSON</param>
        /// <returns></returns>
        [HttpPost] // JSON数据
        public ResultUtils InsertStudent(JObject data)
        {
            try
            {
                return _studentBll.InsertStudent(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertStudent服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 查询所有学生信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllStudent()
        {
            try
            { //使用EF进行查询Student表所有数据
                return _studentBll.GetAllStudent();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllStudent服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改学生信息
        /// </summary>
        /// <param name="data">学生信息实体JSON</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils UpdateStudent(JObject data) {
            try
            {
                return _studentBll.UpdateStudent(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"UpdateStudent服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 获取学生信息
        /// </summary>
        /// <param name="StuId">学号</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetStudent(string StuId) {
            try
            {
                return _studentBll.GetStudent(StuId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetStudent服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据班级查询老师所带班级的学生
        /// </summary>
        /// <param name="token">老师token</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetStudentByClassId(string token) {
            try
            {
                return _studentBll.GetStudentByClassId(token);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetStudentByClassId服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据班级编号查询班级内的学生
        /// </summary>
        /// <param name="classId">班级编号</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllStudentByClassId(string classId) {
            try
            {
                return _studentBll.GetAllStudentByClassId(classId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllStudentByClassId服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
